//
//  YHStatusCell.swift
//  YHWeibo
//
//  Created by 昱含 on 16/5/23.
//  Copyright © 2016年 YuHan. All rights reserved.
//

import UIKit
import SnapKit

class YHStatusCell: UITableViewCell {
    
    var toolbarTopCons: Constraint?

    var statusViewModel : YHStatusViewModel? {
        
        didSet{
            
            statusToolBar.statusViewModel = statusViewModel
            statusOriginalView.statusViewModel = statusViewModel
            
            toolbarTopCons?.uninstall()
            
            if statusViewModel?.status?.retweeted_status == nil {
                statusRetweetedView.statusViewModel = nil
                statusRetweetedView.hidden = true                
                statusToolBar.snp_updateConstraints(closure: { (make) in
                    self.toolbarTopCons = make.top.equalTo(statusOriginalView.snp_bottom).constraint
                })
            } else {
                
                statusRetweetedView.statusViewModel = statusViewModel
                //显示转发微博视图
                statusRetweetedView.hidden = false
                //更新约束 底部工具条的顶部应该相对于 转发微博底部
                statusToolBar.snp_updateConstraints(closure: { (make) in
                   self.toolbarTopCons =  make.top.equalTo(statusRetweetedView.snp_bottom).constraint
                })
            }
        }
    }
    

    override func awakeFromNib() {
        super.awakeFromNib()
    
    }
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        self.selectionStyle = .None
//        self.backgroundColor = randomColor()
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
        contentView.addSubview(statusOriginalView)
        contentView.addSubview(statusToolBar)
        contentView.addSubview(statusRetweetedView)
        
        statusOriginalView.snp_makeConstraints { (make) in
            make.top.left.right.equalTo(contentView)
//            make.height.equalTo(100)
        }
        
        statusRetweetedView.snp_makeConstraints { (make) in
            make.top.equalTo(statusOriginalView.snp_bottom)
            make.left.right.equalTo(contentView)
        }
        
        statusToolBar.snp_makeConstraints { (make) in
            self.toolbarTopCons = make.top.equalTo(statusRetweetedView.snp_bottom).constraint
            make.right.left.equalTo(contentView)
            //设置高度
            make.height.equalTo(36)
        }
        
        contentView.snp_makeConstraints { (make) in
            make.left.top.right.equalTo(self)
            make.bottom.equalTo(statusToolBar.snp_bottom)
        }
        
    }

    override func setSelected(selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

   
    }
    
    private lazy var statusOriginalView : YHStatusView = YHStatusView()
    private lazy var statusToolBar: YHStatusToolBar = YHStatusToolBar()
    private lazy var statusRetweetedView: YHStatusRetweetedView = YHStatusRetweetedView()

}
